<?php
require_once 'aliSign.php';

header('Content-type:text/html; Charset=utf-8');

class aliPay extends aliSign
{

    protected $charset;
    //私钥值
    protected $rsaPrivateKey;
    protected $totalFee;
    protected $outTradeNo;
    protected $orderName;

    public function __construct()
    {
        $this->charset = 'utf-8';
    }

    /**
     * 发起订单【电脑网站支付】
     * @return array
     */
    public function pcPay($order_sn, $price, $title)
    {
        if (empty($order_sn)) apiReturn(2, '订单号缺失');
        if (empty($price)) apiReturn(2, '价格错误');
        if (empty($title)) apiReturn(2, '支付标题缺失');

        //请求参数
        $requestConfigs = array(
            'out_trade_no' => $order_sn,
            'product_code' => 'FAST_INSTANT_TRADE_PAY',
            'total_amount' => $price, //单位 元
            'subject' => $title,  //订单标题
        );
        $commonConfigs = array(
            //公共参数
            'app_id' => $this->getAppid(),
            'method' => 'alipay.trade.page.pay',             //接口名称
            'format' => 'JSON',
            'charset' => $this->charset,
            'sign_type' => 'RSA2',
            'timestamp' => date('Y-m-d H:i:s'),
            'version' => '1.0',
            'notify_url' => $this->getNotifyUrl(),
            'biz_content' => json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
        return $this->buildRequestForm($commonConfigs);
    }


    /**
     * 发起订单【手机网站支付】
     * @return array
     */
    public function mobilePay($order_sn, $price, $title)
    {
        if (empty($order_sn)) apiReturn(2, '订单号缺失');
        if (empty($price)) apiReturn(2, '价格错误');
        if (empty($title)) apiReturn(2, '支付标题缺失');

        //请求参数
        $requestConfigs = array(
            'out_trade_no' => $order_sn,
            'product_code' => 'QUICK_WAP_WAY',
            'total_amount' => $price, //单位 元
            'subject' => $title,  //订单标题
        );
        $commonConfigs = array(
            //公共参数
            'app_id' => $this->getAppid(),
            'method' => 'alipay.trade.wap.pay',             //接口名称
            'format' => 'JSON',
            'charset' => $this->charset,
            'sign_type' => 'RSA2',
            'timestamp' => date('Y-m-d H:i:s'),
            'version' => '1.0',
            'notify_url' => $this->getNotifyUrl(),
            'biz_content' => json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
        return $this->buildRequestForm($commonConfigs);
    }

    /**
     * 建立请求，以表单HTML形式构造（默认）
     * @param $para_temp 请求参数数组
     * @return 提交表单HTML文本
     */
    protected function buildRequestForm($para_temp)
    {

        $sHtml = "正在跳转至支付页面...<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=" . $this->charset . "' method='POST'>";
        foreach ($para_temp as $key => $val) {
            if (false === $this->checkEmpty($val)) {
                $val = str_replace("'", "&apos;", $val);
                $sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
            }
        }
        //submit按钮控件请不要含有name属性
        $sHtml = $sHtml . "<input type='submit' value='ok' style='display:none;''></form>";
        $sHtml = $sHtml . "<script>document.forms['alipaysubmit'].submit();</script>";
        return $sHtml;
    }


}